package com.dodock.banglapapers.server.helper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.*;
import org.jsoup.select.Elements;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class DDNNewsDetailDW {

	
	public String Generate(String newsid) throws IOException{
		//
		// Get a handle on the datastore itself
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();		
		// Lookup data by known key name
		try {
			Entity detailsEntity = datastore.get(KeyFactory.createKey("NewsDetails", "dw__" + newsid));
			Text text = (Text)detailsEntity.getProperty("data");
			return text.getValue();
			
		} catch (EntityNotFoundException e) {
			
			Document doc = Jsoup.connect(Settings.WWW_DW_DE + "/" + newsid).get();
			
			JsonObject news = new JsonObject();
			news.addProperty("id", newsid);
			Element longText = doc.select("div[class=longText]").first();
			unwrapLinks(longText);
			removeTeasers(longText);
			
			news.addProperty("sharelink", Settings.WWW_DW_DE + "/" + newsid);
			
			JsonArray imageGallery = null;
			Elements galleryHolders = doc.select("div[class=artHeadPic]");
			if(galleryHolders != null && galleryHolders.size() > 0){
				imageGallery = new JsonArray();
				Element g = galleryHolders.first();
				Elements galleryImages = g.select("img");
				for(Element img:galleryImages){
					JsonObject image = new JsonObject();
					image.addProperty("url", Settings.WWW_DW_DE + img.attr("src"));
					imageGallery.add(image);
				}
				news.add("gallery", imageGallery);
			}
			
			Elements picBoxes = longText.select("div[class^=picBox");
			if(picBoxes != null && picBoxes.size() > 0){
				if(imageGallery == null){
					imageGallery = new JsonArray();
					news.add("gallery", imageGallery);
				}
				for(Element pb:picBoxes){
					Element img = pb.select("img").get(0);
					JsonObject image = new JsonObject();
					image.addProperty("url", Settings.WWW_DW_DE + img.attr("src"));
					imageGallery.add(image);
					pb.remove();
				}
			}
			
			news.addProperty("details", longText.html());
			
			String data = news.toString();
			Text text = new Text(data);
			/* save here */
			Entity detailsEntity = new Entity("NewsDetails", "dw__" + newsid);
			detailsEntity.setProperty("data", text);
			datastore.put(detailsEntity);
			return data;
		}
	}
	
	private void unwrapLinks(Element parent){
		if(parent != null){
			Elements as = parent.getElementsByTag("a");
			if(as != null){
				as.unwrap();
			}
		}
	}
	
	private void removeTeasers(Element parent){
		if(parent != null){
			Elements as = parent.select("div[class^=imgTeaserM]");
			if(as != null){
				as.remove();
			}
		}
	}
	
}
